<?php
/**
 * polwebgis
 * 
 * This file is part of polwebgis.
 *
 * @copyright   Copyright (c) 2012 Dominik Janisch (http://www.joanneum.at)
 * @lastmodified    $LastChangedDate: 2012-04-02 17:22:08 +0000 (Mon, 02 Apr 2012) $
 * @revision    $Revision: 173 $ 
 */
require_once(PATH_TO_APP . "/components/gismap/MapLayer.php");

class BaseLayer extends MapLayer
{
    
    const MAP_LABEL_REGION = 'region';
    
    const MAP_LABEL_ID = 'region_id';
    
    const MAP_LABEL_EMPTY = 'label_empty';
    
    const MAP_LABEL_ID_PLUS_REGION = 'id_name';
    
    const CLASS_START_COLOR = 'FFDA03';
    
    const CLASS_END_COLOR = 'EA0000';
    

    public function __construct(&$map_list)
    {
        $this->map_list_ = $map_list;
        $this->setLayerDefaultValues();
        $this->setBaseLayerDefaultValues();
    }
    
    public function __destruct(){}
    
    public function setBaseLayerDefaultValues()
    {
        if($this->map_list_->get('map_label') === false)
            $this->map_list_->set('map_label', constant("self::MAP_LABEL_REGION"));
        if($this->map_list_->get('start_color') === false)
            $this->map_list_->set('start_color', constant("self::CLASS_START_COLOR"));
        if($this->map_list_->get('end_color') === false) 
            $this->map_list_->set('end_color', constant("self::CLASS_END_COLOR"));
    }
    
    public function prepareMapserverLayer($layer, $e_id, $e_projection, $data_table, $dt_where_condition)
    {
        if($this->map_list_->get('map_label') !== constant("self::MAP_LABEL_EMPTY"))
            $layer->set('labelitem', $this->map_list_->get('map_label'));
        $layer->set('opacity', $this->map_list_->get('layer_opacity'));
        $this->getBaseLayer($layer, $e_id, $e_projection, $data_table, $dt_where_condition);
    }
    
    public function checkInput($request)
    {
        $this->checkLayerInput($request);
        if($request->issetParameter('post', 'map_label'))
        {
            switch($request->getParameter('post', 'map_label'))
            {
                case constant("self::MAP_LABEL_ID"):
                    $this->map_list_->set('map_label', constant("self::MAP_LABEL_ID"));
                    break;
                case constant("self::MAP_LABEL_EMPTY"):
                    $this->map_list_->set('map_label', constant("self::MAP_LABEL_EMPTY"));
                    break;
                case constant("self::MAP_LABEL_ID_PLUS_REGION"):
                    $this->map_list_->set('map_label', constant("self::MAP_LABEL_ID_PLUS_REGION"));
                    break;
                default:
                    $this->map_list_->set('map_label', constant("self::MAP_LABEL_REGION"));
            }
        }
    }
    
    private function getBaseLayer($layer, $e_id, $e_projection, $data_table, $dt_where_condition)
    {
        $dt_stmt = "(SELECT gid, sum(cases) as cases from ".$data_table;
        if(strlen($dt_where_condition) > 0)
            $dt_stmt .= " where ".$dt_where_condition;
        $dt_stmt .= " group by gid)";
        if($this->map_list_->get('map_label') === constant("self::MAP_LABEL_ID_PLUS_REGION"))
        {
            if($this->map_list_->get('arr_layer_gids') !== false)
            {
                if(count($this->map_list_->get('arr_layer_gids')) > 0)
                {
                    $q = "the_geom from (select the_geom, a.gid, region, region_id, region_id || ' ' || region as id_name, cases from";
                    $q .= " shape_".$e_id." as a left join ".$dt_stmt." as b ON (a.gid=b.gid)";
                    $q .= " where a.gid IN (";
                    $q .= implode(',', $this->map_list_->get('arr_layer_gids'));
                    $q .= ") UNION select the_geom, gid, null as region, null as region_id, null as id_name, cases from"; 
                    $q .= "shape_".$e_id." as c  left join ".$dt_stmt." as d ON (c.gid=d.gid)"; 
                    $q .= "where c.gid NOT IN (".implode(',', $this->map_list_->get('arr_layer_gids')).")) as tmp using unique gid using SRID=".$e_projection;
                    $layer->set('data', $q);
                }
                else
                {
                    $q = "the_geom from (select the_geom, a.gid, null as region, null as region_id, null as id_name, cases from shape_".$e_id." as a left join ".$dt_stmt." as b ON (a.gid=b.gid)) as tmp using unique gid using SRID=".$e_projection;
                    $layer->set('data', $q);
                }
            }
            else
            { 
                $q = "the_geom from (select the_geom, a.gid, region, region_id, region_id || ' ' || region as id_name, cases from shape_".$e_id." as a left join ".$dt_stmt." as b ON (a.gid=b.gid)) as tmp using unique gid using SRID=".$e_projection;
                $layer->set('data', $q);
            }
        }
        else
        {
            if($this->map_list_->get('arr_layer_gids') !== false)
            {
                if(count($this->map_list_->get('arr_layer_gids')) > 0)
                {
                    $q = "the_geom from (select the_geom, a.gid, region, region_id, cases from shape_".$e_id." as a left join ".$dt_stmt." as b ON (a.gid=b.gid) where a.gid IN (".implode(',', $this->map_list_->get('arr_layer_gids')).")"; 
                    $q .= "UNION select the_geom, c.gid, null as region, null as region_id, cases from shape_".$e_id." as c left join ".$dt_stmt." as d ON (c.gid=d.gid) where c.gid NOT IN (".implode(',', $this->map_list_->get('arr_layer_gids')).")) as tmp using unique gid using SRID=".$e_projection;
                    $layer->set('data', $q);
                }
                else
                {
                    $layer->set('data', "the_geom from (select the_geom, gid, null as region, null as region_id, cases from shape_".$e_id." as a left join ".$dt_stmt." as b ON (a.gid=b.gid)) as tmp using unique gid using SRID=".$e_projection);
                }
            }
            else
            {
                $q = "the_geom from (select the_geom, a.gid, region, region_id, cases from shape_".$e_id." as a left join ".$dt_stmt." as b ON (a.gid=b.gid)) as tmp using unique gid using SRID=".$e_projection;
                $layer->set('data', $q);
            }
        }
    
    }
}
?>
